package com.android.accessibility.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;

public class SDUtils {
    public static List<String> getAllSdcardPath() {
        List<String> allSdPath = getAllSdPathFromMount();
        if (allSdPath == null) {
            allSdPath = new ArrayList<String>();
        }
        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())) {
            File external = Environment.getExternalStorageDirectory();
            if (external != null) {
                allSdPath.add(external.getAbsolutePath());
            }
            allSdPath = removeDupDir(allSdPath);
        }
        return allSdPath;
    }

    private static List<String> removeDupDir(List<String> pathList) {
        List<String> removeDupList = new ArrayList<String>();
        if (pathList == null || pathList.size() <= 0) {
            return removeDupList;
        }
        Map<String, String> pathMap = new HashMap<String, String>();
        for (String s : pathList) {
            StatFs fs = new StatFs(s);
            long pathSize = fs.getAvailableBlocks() * fs.getBlockSize();
            pathMap.put(String.valueOf(pathSize), s);
        }
        Collection<String> coll = pathMap.values();
        if (coll != null) {
            for (String s : coll) {
                removeDupList.add(s);
            }
        }
        return removeDupList;
    }

    private static ArrayList<String> getAllSdPathFromMount() {
        String sdcardPathAll = getSDCardPathEx();
        Log.d(Log.TAG, "getSDCardPathEx = " + sdcardPathAll);
        if (TextUtils.isEmpty(sdcardPathAll)) {
            return null;
        }
        String[] splits = sdcardPathAll.split("\\|");
        if (splits == null || splits.length <= 0) {
            return null;
        }
        File file = null;
        ArrayList<String> allSdPath = new ArrayList<String>();
        for (String s : splits) {
            if (!TextUtils.isEmpty(s)) {
                file = new File(s);
                if (file.exists() && !allSdPath.contains(s)) {
                    allSdPath.add(s);
                }
            }
        }
        return allSdPath;
    }

    public static String getSDCardPathEx() {
        String mount = new String();
        try {
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec("mount");
            InputStream is = proc.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            String line;
            BufferedReader br = new BufferedReader(isr);
            while ((line = br.readLine()) != null) {
                if (line.contains("secure"))
                    continue;
                if (line.contains("asec"))
                    continue;

                if (line.contains("fat")) {
                    String columns[] = line.split(" ");
                    if (columns != null && columns.length > 2) {
                        if (columns[1] != null && columns[1].equals("on")) {
                            mount = mount.concat(columns[2] + "|");
                        } else {
                            mount = mount.concat(columns[1] + "|");
                        }
                    }
                } else if (line.contains("fuse")) {
                    String columns[] = line.split(" ");
                    if (columns != null && columns.length > 2) {
                        if (columns[1] != null && columns[1].equals("on")) {
                            mount = mount.concat(columns[2] + "|");
                        } else {
                            mount = mount.concat(columns[1] + "|");
                        }
                    }
                }
            }
        } catch (FileNotFoundException e) {
            Log.d(Log.TAG, "error : " + e);
        } catch (IOException e) {
            Log.d(Log.TAG, "error : " + e);
        }
        return mount;
    }
}
